var ajaxUrl = require('../../../utils/url.js');
var encrypt = require('../../../utils/encrypt.js');

Page({
  data: {
    from: '',
    addressList: [],
    hidden: false
  },
  onLoad: function (options) {
    // console.log(options)
    this.setData({
      from: options.from
    })
    
  },
  onShow () {
    this.fetchData()
  },
  fetchData () {
    const self = this
    self.setData({
      hidden: false
    });
    wx.request({
      method:"POST",
      url: ajaxUrl.addressList(),
      data: {
        token:  getApp().globalData.token
      },
      success: function(res) {
        if(res.data.code === 200){
          self.setData({
            'addressList' : res.data.data,
          })
          let flag = false
          self.data.addressList.forEach(item => {
            if (item.defaultAddressFlag === true) {
              wx.setStorage({
                key:"defaultAddress",
                data: item
              })
              flag = true
            }
          })
          if(!flag){
            wx.setStorage({
              key:"defaultAddress",
              data: self.data.addressList[self.data.addressList.length-1]
            })
          }
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'error',
            duration: 1000,
            mask:true
          })
        }
        setTimeout(function () {
          self.setData({
            hidden: true
          });
        }, 300);
      }
    })
  },
  setDefault (e) {
    const self = this
    let data = e.currentTarget.dataset.obj
    let params = {
      addressId: data.addressId,
      name: data.name,
      contactNumber: data.contactNumber,
      provinceCode: data.provinceCode,
      cityCode: data.cityCode,
      countyCode: data.countyCode,
      streetCode: data.streetCode,
      defaultAddressFlag: true,
      detailedAddress: data.detailedAddress
    }
    params = encrypt.encrypt(params)
    wx.request({
      method: "POST",
      url: ajaxUrl.updateAddress(),
      data: params,
      success: function(res){
        if(res.data.code === 200){
          self.fetchData()
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'error',
            duration: 1000,
            mask:true
          })
        }
      }
    })
  },
  deleteAddress (e) {
    let id = e.currentTarget.dataset.id
    const self = this
    let params ={
      addressId: parseInt(id)
    }
    params = encrypt.encrypt(params)
    wx.request({
      method: "POST",
      url: ajaxUrl.deleteAddress(),
      data: params,
      success: function(res){
        if(res.data.code === 200){
          wx.showToast({
            title: '删除成功！',
            icon: 'succes',
            duration: 1000,
            mask:true
          })
          self.fetchData()
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'error',
            duration: 1000,
            mask:true
          })
        }
      }
    })
  },
  toAddress (e) {
    const self = this
    let data = e.currentTarget.dataset.obj
    wx.setStorage({
      key:"addressData",
      data: data
    })
    wx.navigateTo({
      url: '../address/address?addressId='+data.addressId
    });
  }
})